<template>
  <TitleCard :title="titleCard" :showMore="showMore">
    <div class="backStack-table-wrap">
      <div class="head-top-wrap">
        <div class="form-table-wrap">
          <Form
            ref="form"
            layoutType="grid"
            :optionList="formRow"
            :defaultModel="defaultValue"
          ></Form>
          <div class="operation-wrap">
            <a-button @click="clearAll">清空条件</a-button>
            <a-button @click="toSearch">搜索</a-button>
          </div>
        </div>
        <Tip :tip="tip"></Tip>
      </div>
      <Table
        ref="table"
        :options="fields"
        :tableValue="tableValue"
        :customSlotOpt="slotOpt"
        :otherOpt="customOtherOpt"
        :rowSelection="null"
        @change="initTableValue"
        @actionClick="handlerActionClick"
      ></Table>
    </div>
    <AModal ref="modal" :customOpt="customModalOpt" @ok="handlerOk"></AModal>
    <EditLink
      v-if="isShowEdit"
      @cancel="cancel"
      @addSuccess="addSuccess"
      :id="id"
    ></EditLink>
  </TitleCard>
</template>

<script>
import Table from '../../components/table/index.vue'
import Form from '../../components/formTable/form.vue'
import AModal from '../../components/AModal'
import mixinsBackStack from '@/views/backstageSupport/mixins'
import Tip from '../../components/common/tip.vue'
import EditLink from './components/editLink.vue'
import { fields, slotOpt, formRow } from './configs/configs'
import { getLinkManageList } from '@/api/linkManage.js'

export default {
  mixins: [mixinsBackStack],
  data() {
    return {
      slotOpt,
      fields,
      formRow,
      customOtherOpt: {
        pagination: {
          total: 0,
          pageNum: 1,
          pageSize: 10
        }
      },
      tableValue: [],
      defaultSearchParam: {},
      defaultValue: {},
      tip: '点击修改仅支持修改跳转链接地址',
      isShowEdit:false,
      id:''
    }
  },
  components: {
    Table,
    Form,
    AModal,
    Tip,
    EditLink
  },
  mounted() {
    this.initSearchParams() // 重置搜索的参数
    this.initTableValue()
  },
  methods: {
    async initTableValue() {
      try {
        this.loadingTable(true) // 开启表格的加载动画
        const params = this.handlerBeforeRequestParams()
        const { rows, total } = await getLinkManageList(params) // 请求表格数据接口
        const row = this.handlerDateFormat(rows) // 处理时间格式
        this.handlerResultMappings(rows) // 处理数据的映射关系
        this.tableValue = row
        this.customOtherOpt.pagination.total = total
        this.initSelectedRowIds() // 清空勾选的选项
      } catch (error) {
        this.$apiStatusHandler(99, error.msg) // 请求异常，则全局轻提示
      } finally {
        this.loadingTable(false)
      }
    },
    // 请求前的参数收集
    handlerBeforeRequestParams() {
      const { pagination } = this.$refs.table
      const { pageSize, pageNum } = pagination
      const params = {
        ...this.searchParam,
        pageNum,
        pageSize
      }
      return params
    },
    // 编辑
    edit(row) {
      this.id = row.id
      this.isShowEdit = true
    },
    cancel() {
      this.isShowEdit = false
    },
    addSuccess() {
      this.isShowEdit = false
      this.initTableValue()
    },
  }
}
</script>
